package com.ruoyi.recycle.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.recycle.domain.MiniProgram;
import com.ruoyi.recycle.dto.MiniProgramDTO;
import com.ruoyi.recycle.service.IMiniProgramService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 小程序码Controller
 *
 * @author Wubk
 * @date 2022-05-22
 */
@RestController
@RequestMapping("/program")
@Api(tags = "小程序码")
public class MiniProgramController extends BaseController {
    @Autowired
    private IMiniProgramService miniProgramService;

    /**
     * 查询小程序码列表
     */
    @RequiresPermissions("recycle:program:list")
    @GetMapping("/list")
    public TableDataInfo list(MiniProgram miniProgram) {
        startPage();
        List<MiniProgram> list = miniProgramService.selectMiniProgramList(miniProgram);
        return getDataTable(list);
    }

    /**
     * 分页条件查询
     */
    @PostMapping("/programPageQuery")
    @ApiOperation("分页条件查询")
    public IPage<MiniProgram> programPageQuery(@RequestBody MiniProgramDTO queryDTO) {
        final MiniProgram program = new MiniProgram();
        BeanUtils.copyProperties(queryDTO, program);
        final LambdaQueryWrapper<MiniProgram> wrapper = Wrappers.lambdaQuery(program);
        if (queryDTO.getSearchTimeBegin() != null) {
            wrapper.ge(MiniProgram::getCreatedTime, queryDTO.getSearchTimeBegin());
        } else if (queryDTO.getSearchTimeEnd() != null) {
            wrapper.le(MiniProgram::getCreatedTime, queryDTO.getSearchTimeEnd());
        }
        wrapper.orderByDesc(MiniProgram::getCreatedTime);
        return miniProgramService.page(new Page<MiniProgram>()
                .setCurrent(queryDTO.getCurrentPage())
                .setSize(queryDTO.getPageSize()), wrapper);
    }

    /**
     * 导出小程序码列表
     */
    @RequiresPermissions("recycle:program:export")
    @Log(title = "小程序码", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出小程序码列表")
    public void export(HttpServletResponse response, MiniProgram miniProgram) {
        List<MiniProgram> list = miniProgramService.selectMiniProgramList(miniProgram);
        ExcelUtil<MiniProgram> util = new ExcelUtil<MiniProgram>(MiniProgram.class);
        util.exportExcel(response, list, "小程序码数据");
    }

    /**
     * 获取小程序码详细信息
     */
    @RequiresPermissions("recycle:program:query")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取小程序码详细信息")
    @ApiImplicitParam(name = "id", value = "id", dataType = "Long")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(miniProgramService.selectMiniProgramById(id));
    }

    /**
     * 新增小程序码
     */
    @RequiresPermissions("recycle:program:add")
    @Log(title = "小程序码", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增小程序码")
    public AjaxResult add(@RequestBody MiniProgram miniProgram) {
        return toAjax(miniProgramService.insertMiniProgram(miniProgram));
    }

    /**
     * 修改小程序码
     */
    @RequiresPermissions("recycle:program:edit")
    @Log(title = "小程序码", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改小程序码")
    public AjaxResult edit(@RequestBody MiniProgram miniProgram) {
        return toAjax(miniProgramService.updateMiniProgram(miniProgram));
    }

    /**
     * 删除小程序码
     */
    @RequiresPermissions("recycle:program:remove")
    @Log(title = "小程序码", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation("删除小程序码")
    @ApiImplicitParam(name = "ids", value = "id集合", dataType = "List")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(miniProgramService.deleteMiniProgramByIds(ids));
    }
}
